﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Word;
using Microsoft.Office.Tools.Word.Extensions;
using Word = Microsoft.Office.Interop.Word;
using biolit_config;

namespace UCSD_BioLit
{

   
    public class SmartTagHandlers
    {

        public void AddMarkup_BeforeCaptionShow(object sender, ActionEventArgs e)
        {
            CustSmartTagAction a = (CustSmartTagAction)sender;
            ThisAddIn add = Globals.ThisAddIn;

            int id = a.m_TagID;

            if (OntologyUtils.GetNumMatches(e.Text) < id + 1) // the id is zero based
            {
                a.Caption = null;
            }
            else
            {
                string Caption = OntologyUtils.GetActionCaptions(e.Text)[id];
                a.Caption = Caption;
                
            }           

        }


        public void AllMarkupCaption(object sender, ActionEventArgs e)
        {
            CustSmartTagAction a = (CustSmartTagAction)sender;
            ThisAddIn add = Globals.ThisAddIn;

            if (e.Range.XMLParentNode.NamespaceURI == ThisAddIn.BIOLIT_NS_URI)
            {
                a.Caption = "Apply Same mark-up to all Instances of Term";
            }
            else
            {
                a.Caption = null;
            }

        }



        public void AddMarkup_Click(object sender, ActionEventArgs e)
        {
            CustSmartTagAction a = (CustSmartTagAction)sender;

            OntologyCache.OntologyNode node = OntologyUtils.FindNthMatch(e.Range.Text,a.m_TagID);
             if( node != null )
             {
                 WordCommonFuncs.OntologyMarkup(e.Range, Convert.ToString(GetOntologyID(node.OntologyName)), node.id, node.OntologyName, node.OntologyName);
             }
            
        }
        public void ApplyMarkupToAllInstances(object sender, ActionEventArgs e)
        {
            CustSmartTagAction a = (CustSmartTagAction)sender;
            OntologyCache.OntologyNode node = OntologyUtils.FindNthMatch(e.Range.Text);
            if (node != null)
                WordCommonFuncs.AddMarkupToAll(e.Range.Text, node.id, node.OntologyName);
        }

        public void AddMarkupToAll(object sender, ActionEventArgs e)
        {

            WordCommonFuncs.AddMarkupToAll(e.Range);
            /*
            object missing = System.Type.Missing;
            Word.Range DocRng = Globals.ThisAddIn.Application.ActiveDocument.Content;
            DocRng.Find.ClearFormatting();
            DocRng.Find.Forward = true;
            DocRng.Find.Text = e.Range.Text;

            DocRng.Find.Execute( ref missing, ref missing, ref missing, ref missing, ref missing,
                                 ref missing, ref missing, ref missing, ref missing, ref missing,
                                 ref missing, ref missing, ref missing, ref missing, ref missing);

            while (DocRng.Find.Found)
            {
                Word.XMLNode node = e.Range.XMLParentNode;
                if (node.NamespaceURI == ThisAddIn.BIOLIT_NS_URI)
                {
                    WordCommonFuncs.AddMarkup(DocRng, node.Attributes[1].NodeValue, node.Attributes[2].NodeValue, node.Attributes[2].NodeValue);
                }
                
                DocRng.Find.Execute(ref missing, ref missing, ref missing, ref missing, ref missing,
                                    ref missing, ref missing, ref missing, ref missing, ref missing,
                                    ref missing, ref missing, ref missing, ref missing, ref missing);
            } */

        }
        
        public void PDBMarkup_Click(object sender, ActionEventArgs e)
        {
            WordCommonFuncs.DBMarkup(e.Range, e.Range.Text, "PDB", null);
        }

        public void NCBIMarkup_Click(object sender, ActionEventArgs e)
        {
            WordCommonFuncs.DBMarkup(e.Range, e.Range.Text, "NCBI", null);
        }

        public void UniprotMarkup_Click(object sender, ActionEventArgs e)
        {
            WordCommonFuncs.DBMarkup(e.Range, e.Range.Text, "Uniprot", null);
        }


        public static string GetOntologyID(string ontologyName)
        {
            string ontologyList = null;
            bool localFile = false;
            if (Biolit_Settings.Settings.AvailableOntologies != null && Biolit_Settings.Settings.AvailableOntologies.Count() > 0)
            {
                foreach (OntList onlist in Biolit_Settings.Settings.AvailableOntologies)
                {

                    if (Biolit_Settings.Settings.DownloadedOntologies != null)
                    {
                        foreach (biolit_config.DownloadHistory downloadHistory in Biolit_Settings.Settings.DownloadedOntologies)
                        {
                            if (downloadHistory._OntologyName.Equals(ontologyName, StringComparison.CurrentCultureIgnoreCase))
                            {
                                if (downloadHistory._Source.Equals("Local File", StringComparison.CurrentCultureIgnoreCase))
                                {
                                    localFile = true;
                                    ontologyList = null;
                                    break;
                                }
                            }
                        }
                    }
                    if (localFile)
                        return ontologyList;
                    else
                    {
                        if (onlist.Name.Equals(ontologyName, StringComparison.CurrentCultureIgnoreCase))
                        {
                            ontologyList = onlist.ID;
                            break;
                        }
                    }
                }
                return ontologyList;
            }
            return null;
        }
        
    }
}
